<?php
//$_GET['id']
//$_GET['parent']
//$_GET['confirm']有值则表示需要确认
//
//以id为根的子树移动到parent旗下
//id节点需要delete权限，parent节点需要add权限

	include 'include/function.php';
	
	if(!isset($_user_id))redirect_error(_d('err_need_login'));
	
	$movenode_id=-1;
	$movenode_parent=-1;
	if(isset($_GET['id']))$movenode_id=get_to_mysql($_GET['id']);
	if(isset($_GET['parent']))$movenode_parent=get_to_mysql($_GET['parent']);
	if($movenode_id==-1)redirect_error('Root node cant be moved.');
	if($movenode_parent==-1)redirect_error('Node cant be moved to root.');
	if($movenode_id==$movenode_parent)redirect_error('Node cant be moved to itself.');
	
	//id存在性检测
	$query="select title,parent,child_count from node where id=$movenode_id ";
	$ret=oj_query($query);
	$row=mysql_fetch_row($ret);
	if(!isset($row[0]))redirect_error('Node not found!');
	//delete权限检测
	$movenode_id_title	=$row[0];
	$movenode_id_parent	=$row[1];
	$movenode_id_child	=$row[2];
	permission_check_delete($_user_id,$movenode_id);
	
	//parent存在性检测
	$query="select title from node where id=$movenode_parent ";
	$ret=oj_query($query);
	$row=mysql_fetch_row($ret);
	if(!isset($row[0]))redirect_error('Parent node not found!');
	//add权限检测
	$movenode_parent_title	=$row[0];
	permission_check_add($_user_id,$movenode_parent);
	
	//confirm
	if(!isset($_GET['confirm'])) {
		update_child_count($movenode_id_parent,-$movenode_id_child-1);
		update_child_count($movenode_parent,$movenode_id_child+1);
		
		$query="update node set parent=$movenode_parent, child_update_time=now() where id=$movenode_id ";
		$ret=oj_query($query);
		redirect('shownode.php?id=' . $movenode_id);
	}
	//待确认
	include 'include/header.php';
	echo "<title>$site_title - Move Node $movenode_id_title</title>";
	echo '<center style="font-size:2em;padding-bottom:1em;">';
		echo "Are you sure to move the node-$movenode_id ($movenode_id_title) to node-$movenode_parent ($movenode_parent_title) ?<br>";
		echo 'Child nodes will be moved with the node.<br>';
	echo '</center>';
	echo '<center>';
		echo "<a href='movenode.php?id=$movenode_id&parent=$movenode_parent'>Yes</a>&nbsp;&nbsp;";
		echo "<a href='shownode.php?id=$movenode_id'>Cancel</a>";
	echo '</center>';
	
	include 'include/footer.php';
		
?>

